﻿/// <reference path="editar-recurso-view.js" />
/// <reference path="../../yui-3.14-min.js" />
YUI.add('recurso-list-view', function (Y) {

    Y.RecursosView = Y.Base.create('RecursosView', Y.View, [], {
        loadTemplate: function () {
            debugger
            if (this.template == '') {
                var that = this,
                    request = Y.io('/ExcellentProject/scripts/views/cuestionarios/recursos/templates/recursos.html', {
                        on: {
                            complete: function (id, response) {
                                that.templateIsCharged = true;
                                that.template = response.responseText;

                                try {
                                    $('#table_id_recurso').parent().replaceWith('');
                                } catch (err) {
                                    console.log(err)
                                }

                                that.render();
                            },
                            failure: function (id, response) {
                                alert(response)
                            }
                        }
                    });
            }
        },
        render: function () {
            this.loadTemplate();
            if (this.templateIsCharged) {

                this.get('container').setHTML(this.template);
                //creacion de los modelos
                var list = this.get('modelList');
                debugger
                var table = $('#table_id_recurso').DataTable({
                    "language": {
                        "search": "Buscar:",
                        "lengthMenu": "Items por página: _MENU_",
                        "info": "Items del _START_ al _END_ de _TOTAL_ items.",
                        "emptyTable": "Items no disponibles.",
                        "infoEmpty": "0 ítems.",
                        "paginate": {
                            "next": "Siguiente",
                            "previous": "Anterior",
                            "first": "Primero",
                            "last": "Ultimo"
                        }
                    },
                    "paging": true,
                    retrieve: true,
                    data: list._items,
                    "rowCallback": function (row, aData) {
                        debugger
                        $('td:eq(3)', row).html('<a title="Editar" class="btn btn-warning" href="/recursos/' + aData.get('IdRecurso') + '">' +
                        '<i class="fa fa-pencil">' + '</i>' + '<span class="tooltip"></span></a>');
                        $('td:eq(4)', row).html('<a title="Eliminar" class="btn btn-danger" href="/recursos/eliminar/' + aData.get('IdRecurso') + '" >' +
                        '<i class="fa fa-trash-o">' + '</i>' + '<span class="tooltip"></span></a>');
                    }
                    ,
                    columns: [
                        { data: '_state.data.Nombre.lazy.value' },
                        { data: '_state.data.CodigoTipo.lazy.value', width: '20px' },
                        { data: '_state.data.Uri.lazy.value' },
                         { data: '_state.data.IdRecurso.lazy.value' },
                        { data: '_state.data.IdRecurso.lazy.value' }
                    ]
                });
                table.draw();
            }
        }
    })

}, '0.0.0.1', {
    requires: ['view', 'recurso-model-list', 'recurso-model', 'io', 'handlebars',
        'editar-recurso-view']
})